<template>
<div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <!-- 添加分类按钮 -->
        <el-row>
            <el-col>
                <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
            </el-col>
        </el-row>
        <!-- 分类列表展示区域 -->
        <tree-table :data="cateList" :columns="columns" border show-index index-text="#" :show-row-hover="false" :selection-type="false" :expand-type="false">
            <!-- 是否有效 -->
            <template slot="cat_deleted" slot-scope="scope">
                <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen;"></i>
                <i class="el-icon-error" v-else style="color: red"></i>
            </template>
            <!-- 排序 -->
            <template slot="order" slot-scope="scope">
                <el-tag type="primary" size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
                <el-tag type="success" size="mini" v-else-if="scope.row.cat_level === 1">二级</el-tag>
                <el-tag type="info" size="mini" v-else>三级</el-tag>
            </template>
            <!-- 操作 -->
            <template slot="opts" slot-scope="scope">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditCateDialog(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            </template>
        </tree-table>
        <!-- 分页功能 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.pagenum" :page-sizes="[1, 2, 3, 5, 10]" :page-size="params.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 添加分类对话框 -->
        <el-dialog title="添加分类" :visible.sync="addCateVisible" width="50%" @close="addCateClosed">
            <!-- 添加分类表单 -->
            <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <el-cascader v-model="cascaderValue" :options="parentCateList" :props="cascaderProps" @change="handleChange" clearable change-on-select></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="addCateVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </span>
        </el-dialog>

    </el-card>
</div>
</template>

<script>
export default {
  data() {
    return {
      // 商品分类数据列表
      cateList: [],
      // 获取商品分类数据列表所传参数
      params: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类数据总数
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        type: 'template',
        template: 'cat_deleted'
      }, {
        label: '排序',
        type: 'template',
        template: 'order'
      }, {
        label: '操作',
        type: 'template',
        template: 'opts'
      }],
      // 控制添加分类对话框是否显示
      addCateVisible: false,
      // 添加分类表单数据
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 添加分类表单验证规则
      addCateRules: {
        cat_name: [{
          required: true,
          message: '请输入分类名称',
          trigger: 'blur'
        }]
      },
      // 一二级商品分类数据列表
      parentCateList: [],
      // 级联选择器选中的值
      cascaderValue: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: 'true'
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据列表
    async getCateList() {
      const {
        data: res
      } = await this.$http.get('categories', {
        params: this.params
      })
      if (res.meta.status !== 200) {
        return this.$message({
          message: '获取数据失败',
          type: 'error',
          showClose: true
        })
      }
      this.cateList = res.data.result
      this.total = res.data.total
      // console.log(this.cateList)
    },
    // 监听每页显示数量pagesize改变
    handleSizeChange(pagesize) {
      this.params.pagesize = pagesize
      this.getCateList()
    },
    // 监听当前页pagenum改变
    handleCurrentChange(pagenum) {
      this.params.pagenum = pagenum
      this.getCateList()
    },
    // 点击显示编辑分类对话框
    showEditCateDialog(cate) {

    },
    // 点击显示添加分类对话框
    async showAddCateDialog() {
      const {
        data: res
      } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error('获取一二级商品数据失败！')
      this.parentCateList = res.data
      // console.log(this.parentCateList);
      // 显示添加分类对话框
      this.addCateVisible = true
    },
    // 监听添加分类对话框关闭事件
    addCateClosed() {
      this.$refs.addCateRef.resetFields()
      this.cascaderValue = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    // 父级分类的级联选择器选中值改变
    handleChange(val) {
      this.cascaderValue = val
      if (val.length > 0) {
        this.addCateForm.cat_pid = val[val.length - 1]
        this.addCateForm.cat_level = val.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      // console.log(this.addCateForm);
      // console.log(this.cascaderValue);
    },
    // 点击确定添加分类功能
    addCate() {
      this.$refs.addCateRef.validate(async (value) => {
        if (!value) return this.$message.error('格式不正确或内容为空！')
        const {
          data: res
        } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败！')
        this.$message.success('添加分类成功！')
        this.getCateList()
        this.addCateVisible = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
    width: 100%;
}
</style>
